package com.blb.ssh.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.blb.ssh.dao.UserDao;
import com.blb.ssh.entity.Menu;
import com.blb.ssh.entity.Role;
import com.blb.ssh.entity.User;
import com.blb.ssh.exception.BusinessException;
import com.blb.ssh.service.UserService;


	public class UserSerciceImpl implements UserService {
		
		private UserDao userDao;
			
		public void setUserDao(UserDao userDao) {
			this.userDao = userDao;
		}

		public User login(String username, String password)throws BusinessException{
			User user=userDao.getUserByUserName(username);
			if(user==null){
				throw new BusinessException("用户名不存在");
			}
			if(!user.getPassword().equals(password)){
				throw new BusinessException("密码错误");
			}
			return user;
		}
		
		
		public Long getUserCount(){
			return userDao.getUserCount();
		}
		
		public List<User> getUserListByMap(int page, int rows){
			return userDao.getUserListByMap(page,rows);
		}
		
		public boolean batchDeleteUser(String ids){
			String[]  idstr=StringUtils.split(ids, ",");
			return	userDao.batchDeleteUser(idstr);
		}
		
		public boolean addUser(User user){
			User u=userDao.getUserByUserName(user.getUsername());
			if(u !=null){
				boolean isok=false;
				return isok;
			}
			return userDao.addUser(user);
		}
		
		public boolean editUser(User user){
			return userDao.editUser(user);
		}
		
		public List<Role> getAllRoleList(){
			return userDao.getAllRoleList();
		}
		
		//按钮控制
		public List<Menu> buttonControl(int menui){
			HttpSession session=ServletActionContext.getRequest().getSession();
			int roleId=(int)session.getAttribute("Roleid");
			return userDao.buttonControl(roleId,menui);
		}
	}
